package ai4games_tests.tests;

import game2dai.World;
import game2dai.Domain;
import game2dai.entities.Obstacle;
import game2dai.entities.Vehicle;
import game2dai.entityshapes.ps.ArrowPic;
import game2dai.entityshapes.ps.Hints;
import game2dai.entityshapes.ps.ObstaclePic;
import game2dai.maths.Vector2D;
import game2dai.steering.AutoPilot;
import game2dai.steering.SBF;
import processing.core.PApplet;


/**
 * Testing the hide behaviour
 * 
 * @author Peter
 *
 */
public class GameTest_008 extends PApplet {

	final boolean HINTS = false;
	
	int[] obs = {
			100, 100, 16,  
			230, 90, 22,   
			440, 110, 9,
			300, 190, 35,
			450, 220 , 19,
			200, 350, 11,
			290, 320, 15,
			400, 340, 17,
			270, 410, 22,
			120, 480, 22,
			100, 330, 15,
			480, 415, 16,
			355, 455, 21,
			230, 520, 24			
	};
	World w;
	Obstacle ob = null;
	ObstaclePic obView;
	
	
	ArrowPic seekerView, h0View, h1View;
	Vehicle seeker, hider0, hider1;
	AutoPilot seekerSB, h0SB, h1SB;

//	Vector2D home;
	
	long ct, et, lt;
	float deltaTime;

	public void setup(){
		size(600,600);
		w = new World(width, height);

		for(int i = 0; i < obs.length; i += 3){
			obView = new ObstaclePic(this);
			ob = new Obstacle("", new Vector2D(obs[i+1], obs[i]), obs[i+2]);
			ob.renderer(obView);
			w.add(ob);
		}
		Domain wd = new Domain(0,0,width,height);
		
		seekerSB = new AutoPilot();
		seekerSB.wanderOn().obstacleAvoidOn();
		seeker = new Vehicle(new Vector2D(30, 0), // position
				20, // collision radius
				new Vector2D(20,20), // velocity
				40, // maximum speed
				new Vector2D(1,1), // heading
				1, // mass
				1, // turning rate
				200 // max force
				); 
		seekerView = new ArrowPic(this);	
		seekerView.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY);
		seeker.renderer(seekerView);
		seeker.AP(seekerSB);
		seeker.worldDomain(wd);
		w.add(seeker);
		
		// Yellow hider
		h0SB = new AutoPilot();
		h0SB.hideOn(seeker).obstacleAvoidOn();
//		h0SB.setAgent(SBF.AGENT_TO_EVADE, seeker);
		hider0 = new Vehicle(new Vector2D(300,30), // position
				20, // collision radius
				new Vector2D(20,20), // velocity
				100, // maximum speed
				new Vector2D(1,1), // heading
				1, // mass
				1, // turning rate
				200 // max force
				); 
		h0View = new ArrowPic(this);	
		h0View.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY);
		hider0.renderer(h0View);
		hider0.AP(h0SB);
		hider0.worldDomain(wd);
		w.add(hider0);
		
		// Green hider
		h1SB = new AutoPilot();
		h1SB.hideOn(seeker).obstacleAvoidOn();
		hider1 = new Vehicle(new Vector2D(550,290), // position
				20, // collision radius
				new Vector2D(20,20), // velocity
				100, // maximum speed
				new Vector2D(1,1), // heading
				1, // mass
				1, // turning rate
				200 // max force
				); 
		h1View = new ArrowPic(this);	
		h1View.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY);
		hider1.renderer(h1View);
		hider1.AP(h1SB);
		hider1.worldDomain(wd);
		w.add(hider1);
		
		ct = lt = millis();	
		ellipseMode(CENTER);
	}

	public void draw(){
		ct = millis();
		et = ct - lt;
		lt = ct;
		deltaTime = et / 1000.0f;

		background(220,255,220);
		fill(0,64,0);
		noStroke();
		
//		Vector2D h0target = h0SB.getTarget(SBF.ARRIVE);
//		if(h0target != null){
//			fill(255,255,128);
//			ellipse((float)h0target.x, (float)h0target.y, 14,14);
//		}
//		Vector2D h1target = h1SB.getTarget(SBF.ARRIVE);
//		if(h1target != null){
//			fill(128,255,128);
//			ellipse((float)h1target.x, (float)h1target.y, 14,14);
//		}

		w.update(deltaTime);
		w.draw();
		
	}

}
